import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/widgets/bottom_sheet/add_discount_bottom_sheet.dart';

mixin DiscountMixin<T> on BaseProvider<T> {
  String get discountType => discountValue == null
      ? null
      : discountValue.endsWith('%')
          ? 'percentage'
          : 'value';

  String discountValue;

  double get total;

  double get discountAmount {
    if (discountValue == null) return 0.0;
    double discount = 0.0;
    if (discountValue.endsWith('%')) {
      discount =
          double.parse(discountValue.substring(0, discountValue.length - 1));
    } else {
      discount = double.parse(discountValue, (e) => 0.0);
    }
    return discount;
  }

  double get discount {
    if (discountValue == null) return null;
    double discountAmount = 0.0;
    if (discountValue.endsWith('%')) {
      discountAmount = total *
          double.parse(discountValue.substring(0, discountValue.length - 1)) /
          100;
    } else {
      discountAmount = double.parse(discountValue, (e) => 0.0);
    }
    return discountAmount;
  }

  void onAddDiscount() {
    obtainContext?.call((context) async {
      final result = await showAddDiscountBottomSheet(context, discountValue);
      if (result != null) {
        if (result is bool) {
          discountValue = null;
        } else {
          discountValue = result;
        }
      }
      update();
    });
  }
}
